﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class TurnSystem : MonoBehaviour {

	public int __TURN_NUMBER;

	public Text __TEXT_OBJECT;

	public HouseGenerator _houseGenerator;

	public List<FireMan> _fireManz = new List<FireMan>();

	public List<House> _houses = new List<House>();

	public ScoreKeeper _scoreKeeper;

	private Queue<EOTitem> EOTqueue;



	//
	public HashSet<FireMan> mSelectedFireMen = new HashSet<FireMan> ();

	public HashSet<FireTruck> mSelectedFireTruck = new HashSet<FireTruck> ();

	public House mSelectedHouse; //  = new List<Houses> ();

	public GameObject soundman;

	private float nextTime = 0;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {


		if ((EOTqueue != null) && (EOTqueue.Count>0))
		{
			if (!EOTqueue.Peek().already)
			{
				nextTime = Time.time + EOTqueue.Peek().waittime;
				EOTqueue.Peek().already = true;
			}

			if (Time.time > nextTime)
			{
				EOTqueue.Peek().runitem();
				EOTqueue.Dequeue();
				if (EOTqueue.Count ==0)
				{
					__TURN_NUMBER++;
					updateUI ();
				}
			}

		}

	
	}


	public void ToggleSelection(FireTruck fireTruck) {
		if (fireTruck.IsSelected()) {
			UnSelect(fireTruck);
		} else {
			Select(fireTruck);
		}
	}

	public void UnSelect(FireTruck fireTruck) {
		mSelectedFireTruck.Remove (fireTruck);
		fireTruck.Unselect ();
	}

	public void Select (FireTruck fireTruck) {
		mSelectedFireTruck.Add (fireTruck);
		fireTruck.Select ();
	}

	public void toggleSelection(FireMan fireMan) {
		if (fireMan.IsSelected()) {
			unSelectFireMan(fireMan);
		} else {
			selectFireMan(fireMan);
		}
	}

	public void selectFireMan(FireMan fireMan) {

		fireMan.Select ();
		mSelectedFireMen.Add(fireMan);

		Debug.Log("fire man add count" + mSelectedFireMen.Count );

	}

	public void unSelectFireMan(FireMan unSelectFireMan) {
		unSelectFireMan.Unselect ();
		mSelectedFireMen.Remove (unSelectFireMan);

		Debug.Log("fire man remove count" + mSelectedFireMen.Count );

	}

	public void selectHouse(House house) {
		house.Select ();
		mSelectedHouse = house;
		Debug.Log("selecting house  Xxxx" + mSelectedHouse);
	}

	public void UnselectHouse(House house) {
		house.Unselect ();
		if (mSelectedHouse = house) {
			mSelectedHouse = null;
		}
	}

	public void endTurn() {

		upateFire ();

	}

	public void updateUI() {
		__TEXT_OBJECT.text = __TURN_NUMBER.ToString ();
	}


	public void moveFireMenToHouse() {

		Debug.Log("toggling selecting fireman" + mSelectedFireMen.Count);

		Debug.Log("selected house " + mSelectedHouse == null);

		// remove fire men to the houses

		if (mSelectedHouse != null)  {

			// make sure you are not removing from the same collection that you iterationg over
			HashSet<FireMan> fireMen = new HashSet<FireMan>(mSelectedFireMen);
			foreach (FireMan fireMan in fireMen) {
				Debug.Log("position " + mSelectedHouse.transform.position.ToString());
				fireMan.transform.position = mSelectedHouse.transform.position;
				soundman.GetComponent<Soundman>().playsound(5);
				mSelectedHouse.getFireMen().Add(fireMan);

				unSelectFireMan(fireMan);

			}


			HashSet<FireTruck> fireTrucks = new HashSet<FireTruck>(mSelectedFireTruck);

			Debug.Log ("Fire Trucks " + fireTrucks.Count);

			foreach (FireTruck fireTruck in fireTrucks ) {

				if (fireTruck.AssignedHouse != null) {
					fireTruck.AssignedHouse.RemoveFireTruck(fireTruck);
				}

				Debug.Log("position " + mSelectedHouse.transform.position.ToString());
				fireTruck.transform.position = mSelectedHouse.transform.position;
				soundman.GetComponent<Soundman>().playsound(2);
				mSelectedHouse.AddFireTruck(fireTruck);
				
				UnSelect(fireTruck);
				
			}



		}




		/*
		if (mSelectedHouse != null)  {
			mSelectedHouse.getFireMen().AddRange(mSelectedFireMen);
		}*/
	}


	// //
	// //
	// //

	public void UnselectAll() {
		foreach (House house in _houses) {
			house.Unselect ();
		}
	}
	
	public void upateFire() {
				
		HashSet<House> housesToBeOnFire = new HashSet<House> ();
		HashSet<House> houseNextToFire = new HashSet<House> ();
		
		foreach (House house in _houses) {
			if (house.isOnFire()) {//should be fire engine
				//house.Neighbours.ForEach(delegate (House houseToBeFire) {
				//	housesToBeOnFire.Add(houseToBeFire);
				//});
				if (house.FireTrucks.Count == 0) {
					foreach (House h in house.Neighbours) {
						housesToBeOnFire.Add (h);
					}
				}
				foreach (House h in house.Neighbours) {
					houseNextToFire.Add (h);
				}
			}
		}

		EOTqueue = new Queue<EOTitem> ();

		//
		//rescue neighbours not on fire
		//

		foreach (House house in houseNextToFire) {
			if(!house.isOnFire())//non onfire rescues
			{

				if(house.occupantsno >0) //nearest houses to be rescued
				{
					EOTitem thisEOT = new EOTitem(0.3f, true,false,false,soundman.GetComponent<Soundman>(),4,house,_scoreKeeper);

					EOTqueue.Enqueue(thisEOT);

				}

				if(house.getFireMen().Count >0)//extra fireman rescues
				{
					EOTitem rthisEOT = new EOTitem(0.3f, true,false,false,soundman.GetComponent<Soundman>(),4,house,_scoreKeeper);
					
					EOTqueue.Enqueue(rthisEOT);
					//shortwait
					//graphic
					//update score
					//soundman.GetComponent<Soundman>().playsound(4);

				}
				
								
			}
		}

		//
		//kill in burning houses
		//

		foreach (House house in _houses) {
			if(house.isOnFire() && house.occupantsno > 0) {

				EOTitem thisEOT = new EOTitem(0.3f, false,true,false,soundman.GetComponent<Soundman>(),3,house,_scoreKeeper);
				EOTqueue.Enqueue(thisEOT);

				//soundman.GetComponent<Soundman>().playsound(3);
				//Graphic

				//long wait
			}

			if(house.occupantsno > 0 && (house.getFireMen().Count > 0))//fire rescues
			{
				EOTitem fthisEOT = new EOTitem(0.3f, true,false,false,soundman.GetComponent<Soundman>(),4,house,_scoreKeeper);//not multifireman friendly

				for (int actionCount = 0; actionCount < house.getFireMen().Count; actionCount++) {
					EOTqueue.Enqueue(fthisEOT);
				}
				
				//house.UpdateOccs();
				//graphic
				//update score
				//soundman.GetComponent<Soundman>().playsound(4);
			}

		}

		//
		// Spread Fire to all the other houses
		//
		foreach (House fireHouse in housesToBeOnFire) {

			//fireHouse.startFire();0
			if(!fireHouse.isOnFire())
			{
				EOTitem hfthisEOT = new EOTitem(0.3f, false,false,true,soundman.GetComponent<Soundman>(),6,fireHouse,_scoreKeeper);
				EOTqueue.Enqueue(hfthisEOT);
			}
			
		}

		int count = 0;

		foreach (House h in _houses) {
			count += h.occupantsno;
		}

		if (count < 10) {
			Application.LoadLevel("endMenu");
		}

	}


	public void GoToMainMenu() {
		Application.LoadLevel ("mainMenu");
	}


}
